<template>
  <!-- 跑马灯与机器人 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="robotDataList" @query="getRunningList(true)" @reset="refresh">
        <template v-slot:row></template>
        <template v-slot:actions>
          <el-button type="primary" v-permission="'robot.add'" size="mini" icon="el-icon-plus" @click="isShowDetails=true,checkedItem=null">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getRunningList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" prop="a_robot_nametype" label="游戏类型" width="150">
            <template slot-scope="scope">
              <span>{{ gameType(scope.row.a_robot_nametype) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_room" label="房间号" width="100">
            <template slot-scope="scope">
              <span> 【{{scope.row.a_robot_room}}】号房间 </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_name" label="机器人名称"/>
          <el-table-column align="center" prop="a_robot_type" label="机器人类型" width="150">
            <template slot-scope="scope">
              <el-tag :type="scope.row.a_robot_type==1?'danger':'warning'">{{ scope.row.a_robot_type==1?'提现':'赢钱' }} </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_money_1" label="提现金额">
            <template slot-scope="scope">
              <span v-if="scope.row.a_robot_money_1"> {{scope.row.a_robot_money_1[0]}} — {{scope.row.a_robot_money_1[1]}}</span>
              <span v-else></span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_money_2" label="赢钱金额">
           <template slot-scope="scope">
              <span v-if="scope.row.a_robot_money_2"> {{scope.row.a_robot_money_2[0]}} — {{scope.row.a_robot_money_2[1]}}</span>
              <span v-else></span>
            </template>
          </el-table-column>
          <el-table-column align="left" prop="operation" label="操作" width="200" fixed="right">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'robot.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)"><i class="el-icon-edit-outline" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'robot.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click.stop="deleteItem(scope.row)"><i class="el-icon-delete" /></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getRunningList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" :data="checkedItem" @onHideDetails="onHideDetails" :robotType="listQuery.game_id"/>
    </div>
  </div>
</template>

<script>
import { throttle } from "@/utils/index";
import { 
  getRunning,
  delRunning,
 } from "@/api/game";
import {
  GAME_TYPE
} from '@/utils/enum'
import PageDetails from "./page-details";
import SearchTable from "@/components/SearchTable";
export default {
  name: "running",
  components: {
    PageDetails,
    SearchTable,
  },
  data () {
    return {
      GAME_TYPE,
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
      },
      typeData:[],
      robotDataList:[]
    };
  },
  created () {
    // this.getRunningList()
  },
  activated () {
    this.getRunningList()
  },
  methods: {
    // 跑马灯与机器人
    async getRunningList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.robotDataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await getRunning(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach((item) => {
            item.isChecked = false;
            if(item.a_robot_money_1){
              item.a_robot_money_1=JSON.parse(item.a_robot_money_1).map(second=>{
                return second=that.$public.mathNumberDivideWan(second)
              })
            }
            if(item.a_robot_money_2){
              item.a_robot_money_2=JSON.parse(item.a_robot_money_2).map(second=>{
                return second=that.$public.mathNumberDivideWan(second)
              })
            }
          });
          that.currentTotal = response.data.total;
          that.robotDataList = response.data.list;
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false;
    },
    // 编辑
    editItem (row) {
      let that = this;
      that.checkedItem = row;
      that.isShowDetails = true;
    },
    // 删除
    deleteItem (row) {
      let that = this;
      that.$confirm(`是否删除【${row.a_robot_name}】机器人`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
          delRunning({
            id: row.id,
          }).then(function () {
            that.getRunningList();
            that.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => { });
    },
    // 游戏类型展示
    gameType(cellValue){
      return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
    },
    // 游戏类型展示
    robotLabel(cellValue){
      return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false;
      if (show) {
        this.getRunningList();
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
      };
      this.getRunningList("");
    }, 2000),
  },
};
</script>

<style lang="scss" scoped></style>
